package advertising

import (
	"context"

	"advertising/config/mysql"
	"advertising/internal/app/grpc/protoc/advertisingc"
	"advertising/internal/app/web/service"
)

// SupportActivityCreate 创建支持活动
func (s *Server) SupportActivityCreate(ctx context.Context, req *advertisingc.SupportActivityCreateReq) (*advertisingc.SupportActivityCreateRsp, error) {
	res := &advertisingc.SupportActivityCreateRsp{}

	// 参数验证
	if req.Name == "" {
		res.Code = -1
		res.Msg = "活动名称不能为空"
		return res, nil
	}

	if req.StartTime == 0 {
		res.Code = -1
		res.Msg = "开始时间不能为空"
		return res, nil
	}

	if req.EndTime == 0 {
		res.Code = -1
		res.Msg = "结束时间不能为空"
		return res, nil
	}

	if req.EndTime <= req.StartTime {
		res.Code = -1
		res.Msg = "结束时间不能小于等于开始时间"
		return res, nil
	}

	db := mysql.NewDB()
	id, err := service.SupportActivityService{}.Create(
		ctx,
		db,
		req.Name,
		req.StartTime,
		req.EndTime,
		req.MaterialIds,
		req.UserId,
		req.Username,
	)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "创建成功"
	res.Id = id
	return res, nil
}

// SupportActivityUpdate 更新支持活动
func (s *Server) SupportActivityUpdate(ctx context.Context, req *advertisingc.SupportActivityUpdateReq) (*advertisingc.SupportActivityUpdateRsp, error) {
	res := &advertisingc.SupportActivityUpdateRsp{}

	// 参数验证
	if req.Id == "" {
		res.Code = -1
		res.Msg = "活动ID不能为空"
		return res, nil
	}

	if req.Name == "" {
		res.Code = -1
		res.Msg = "活动名称不能为空"
		return res, nil
	}

	if req.StartTime == 0 {
		res.Code = -1
		res.Msg = "开始时间不能为空"
		return res, nil
	}

	if req.EndTime == 0 {
		res.Code = -1
		res.Msg = "结束时间不能为空"
		return res, nil
	}

	if req.EndTime <= req.StartTime {
		res.Code = -1
		res.Msg = "结束时间不能小于等于开始时间"
		return res, nil
	}

	db := mysql.NewDB()
	err := service.SupportActivityService{}.Update(
		ctx,
		db,
		req.Id,
		req.Name,
		req.StartTime,
		req.EndTime,
		req.MaterialIds,
	)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "更新成功"
	return res, nil
}

// SupportActivityDelete 删除支持活动
func (s *Server) SupportActivityDelete(ctx context.Context, req *advertisingc.SupportActivityDeleteReq) (*advertisingc.SupportActivityDeleteRsp, error) {
	res := &advertisingc.SupportActivityDeleteRsp{}

	// 参数验证
	if req.Id == "" {
		res.Code = -1
		res.Msg = "活动ID不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	err := service.SupportActivityService{}.Delete(ctx, db, req.Id)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "删除成功"
	return res, nil
}

// SupportActivityDetail 获取活动详情
func (s *Server) SupportActivityDetail(ctx context.Context, req *advertisingc.SupportActivityDetailReq) (*advertisingc.SupportActivityDetailRsp, error) {
	res := &advertisingc.SupportActivityDetailRsp{}

	// 参数验证
	if req.Id == "" {
		res.Code = -1
		res.Msg = "活动ID不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	activity, err := service.SupportActivityService{}.Detail(ctx, db, req.Id)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	if activity == nil {
		res.Code = -1
		res.Msg = "活动不存在"
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.Data = &advertisingc.SupportActivityDetailRsp_SupportActivityDetail{
		Id:            activity.ID,
		Name:          activity.Name,
		StartTime:     activity.StartTime,
		EndTime:       activity.EndTime,
		MaterialIds:   activity.MaterialIDs,
		Status:        int32(activity.Status),
		CreateUser:    activity.CreateUser,
		CreateUsername: activity.CreateUserName,
		CreatedAt:     activity.CreatedAt,
		UpdatedAt:     activity.UpdatedAt,
		DeletedAt:     activity.DeletedAt,
	}

	return res, nil
}

// SupportActivityList 获取支持活动列表
func (s *Server) SupportActivityList(ctx context.Context, req *advertisingc.SupportActivityListReq) (*advertisingc.SupportActivityListRsp, error) {
	res := &advertisingc.SupportActivityListRsp{}

	// 参数验证
	if req.StartTime > 0 && req.EndTime > 0 && req.EndTime < req.StartTime {
		res.Code = -1
		res.Msg = "结束时间不能早于开始时间"
		return res, nil
	}
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.Limit <= 0 {
		req.Limit = 10
	}

	db := mysql.NewDB()
	activities, count, err := service.SupportActivityService{}.List(
		ctx,
		db,
		req.Name,
		req.Id,
		int8(req.Status),
		req.CreateUser,
		req.StartTime,
		req.EndTime,
		int(req.Page),
		int(req.Limit),
	)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.Data = &advertisingc.SupportActivityListRsp_SupportActivityListData{
		Count: count,
		List:  make([]*advertisingc.SupportActivityListRsp_SupportActivityListData_SupportActivityItem, 0),
	}

	for _, activity := range activities {
		res.Data.List = append(res.Data.List, &advertisingc.SupportActivityListRsp_SupportActivityListData_SupportActivityItem{
			Id:            activity.ID,
			Name:          activity.Name,
			StartTime:     activity.StartTime,
			EndTime:       activity.EndTime,
			MaterialIds:   activity.MaterialIDs,
			Status:        int32(activity.Status),
			CreateUser:    activity.CreateUser,
			CreateUsername: activity.CreateUserName,
			CreatedAt:     activity.CreatedAt,
			UpdatedAt:     activity.UpdatedAt,
			DeletedAt:     activity.DeletedAt,
		})
	}

	return res, nil
}

// SupportActivityChangeStatus 修改活动状态
func (s *Server) SupportActivityChangeStatus(ctx context.Context, req *advertisingc.SupportActivityChangeStatusReq) (*advertisingc.SupportActivityChangeStatusRsp, error) {
	res := &advertisingc.SupportActivityChangeStatusRsp{}

	// 参数验证
	if req.Id == "" {
		res.Code = -1
		res.Msg = "活动ID不能为空"
		return res, nil
	}

	if req.Status != 1 && req.Status != 2 {
		res.Code = -1
		res.Msg = "状态值错误，只能为1(启用)或2(禁用)"
		return res, nil
	}

	db := mysql.NewDB()
	err := service.SupportActivityService{}.ChangeStatus(ctx, db, req.Id, int8(req.Status))

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "状态修改成功"
	return res, nil
}

// SupportActivitySameNameExist 检查同名活动是否存在
func (s *Server) SupportActivitySameNameExist(ctx context.Context, req *advertisingc.SupportActivitySameNameExistReq) (*advertisingc.SupportActivitySameNameExistRsp, error) {
	res := &advertisingc.SupportActivitySameNameExistRsp{}

	// 参数验证
	if req.Name == "" {
		res.Code = -1
		res.Msg = "活动名称不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	count := service.SupportActivityService{}.SameNameExist(ctx, db, req.Name, req.ExceptId)

	res.Code = 0
	res.Msg = "检查成功"
	res.Count = count
	return res, nil
}

// SupportActivityGetActivityListByMaterialIDs 根据素材ID获取关联的活动列表
func (s *Server) SupportActivityGetActivityListByMaterialIDs(ctx context.Context, req *advertisingc.SupportActivityGetActivityListByMaterialIDsReq) (*advertisingc.SupportActivityGetActivityListByMaterialIDsRsp, error) {
	res := &advertisingc.SupportActivityGetActivityListByMaterialIDsRsp{}

	// 参数验证
	if req.MaterialId == "" {
		res.Code = -1
		res.Msg = "素材ID不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	activities, err := service.SupportActivityService{}.GetActivityListByMaterialIDs(ctx, db, req.MaterialId)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.List = make([]*advertisingc.SupportActivityGetActivityListByMaterialIDsRsp_SupportActivityItem, 0)

	for _, activity := range activities {
		res.List = append(res.List, &advertisingc.SupportActivityGetActivityListByMaterialIDsRsp_SupportActivityItem{
			Id:            activity.ID,
			Name:          activity.Name,
			StartTime:     activity.StartTime,
			EndTime:       activity.EndTime,
			MaterialIds:   activity.MaterialIDs,
			Status:        int32(activity.Status),
			CreateUser:    activity.CreateUser,
			CreateUsername: activity.CreateUserName,
			CreatedAt:     activity.CreatedAt,
			UpdatedAt:     activity.UpdatedAt,
			DeletedAt:     activity.DeletedAt,
		})
	}

	return res, nil
}

// SupportActivityGetMaterialIDsList 获取活动关联的素材ID列表
func (s *Server) SupportActivityGetMaterialIDsList(ctx context.Context, req *advertisingc.SupportActivityGetMaterialIDsListReq) (*advertisingc.SupportActivityGetMaterialIDsListRsp, error) {
	res := &advertisingc.SupportActivityGetMaterialIDsListRsp{}

	// 参数验证
	if len(req.Ids) == 0 {
		res.Code = -1
		res.Msg = "活动ID列表不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	materialIDsList, err := service.SupportActivityService{}.GetMaterialIDsList(ctx, db, req.Ids)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "获取成功"
	res.List = make([]*advertisingc.SupportActivityGetMaterialIDsListRsp_SupportActivityMaterialIDsItem, 0)

	for _, item := range materialIDsList {
		res.List = append(res.List, &advertisingc.SupportActivityGetMaterialIDsListRsp_SupportActivityMaterialIDsItem{
			Id:          item.ID,
			MaterialIds: item.MaterialIDs,
		})
	}

	return res, nil
}

// SupportActivitySetMaterialIDs 更新活动素材ID
func (s *Server) SupportActivitySetMaterialIDs(ctx context.Context, req *advertisingc.SupportActivitySetMaterialIDsReq) (*advertisingc.SupportActivitySetMaterialIDsRsp, error) {
	res := &advertisingc.SupportActivitySetMaterialIDsRsp{}

	// 参数验证
	if req.Id == "" {
		res.Code = -1
		res.Msg = "活动ID不能为空"
		return res, nil
	}

	db := mysql.NewDB()
	err := service.SupportActivityService{}.SetMaterialIDs(ctx, db, req.Id, req.MaterialIds)

	if err != nil {
		res.Code = -1
		res.Msg = err.Error()
		return res, nil
	}

	res.Code = 0
	res.Msg = "更新成功"
	return res, nil
}

